package org.bjtu.action;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.bjtu.Global;
import org.bjtu.model.Memberlevel;
import org.bjtu.model.Membertype;

import com.janus.util.RequestHelper;


public class UserPermissionAction extends DispatchAction {

	public ActionForward showUserRightList(ActionMapping mapping,ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response){
		System.out.println("show right list");
		RequestHelper rh = new RequestHelper(request);
		long selectUserType = rh.getlongValue("selectUserType", 0);
		String selectUserLevel = rh.getStringValue("selectUserLevel", "0");
		System.out.println("typeId = " + selectUserType + "; levelName = " + selectUserLevel);
		
		Memberlevel[] list = Global.getUserPermissionService().getTypeLevelList(selectUserType, selectUserLevel);
		Membertype[] typeList = Global.getUserPermissionService().getTypeList();
		String[] levelList = Global.getUserPermissionService().getLevelList();
		System.out.println("typeLevelList length = " + list.length);
		System.out.println("level list size = " + levelList.length);
		request.setAttribute("typeLevelList", list);
		request.setAttribute("typeList", typeList);
		request.setAttribute("levelList", levelList);
		return mapping.findForward("list");
	}
	
	public ActionForward deleteType(ActionMapping mapping,ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response){
		System.out.println("delete type");
		Long typeId = Long.parseLong(request.getParameter("typeId"));
		Global.getUserPermissionService().deleteType(typeId);
		try {
			response.sendRedirect("/userManage/userPermissionAction.do?method=showUserRightList");
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return null;
	}
	
	public ActionForward deleteLevel(ActionMapping mapping,ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response){
		System.out.println("delete level");
		Long levelId = Long.parseLong(request.getParameter("levelId"));
		Global.getUserPermissionService().deleteLevel(levelId);
		try {
			response.sendRedirect("/userManage/userPermissionAction.do?method=showUserRightList");
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return null;
	}
	
	public ActionForward editType(ActionMapping mapping,ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response){
		System.out.println("edit type");
		String name = request.getParameter("editUserTypeName");
		Long id = Long.parseLong(request.getParameter("editUserTypeId"));
		System.out.println(id + "  " + name);
		Global.getUserPermissionService().updateType(id, name);
		try {
			response.sendRedirect("/userManage/userPermissionAction.do?method=showUserRightList");
		} catch (Exception e) {
			System.out.println("exception~~~~~~~~~~~~~~");
//			throw new RuntimeException(e);
		}
		return null;
	}
	
	public ActionForward editLevel(ActionMapping mapping,ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response){
		System.out.println("edit level");
		String name = request.getParameter("editUserLevelName");
		RequestHelper r = new RequestHelper(request);
		long id = r.getlongValue("editUserLevelId");
		System.out.println(id + "  " + name);
		Global.getUserPermissionService().updateLevel(id, name);
		try {
			response.sendRedirect("/userManage/userPermissionAction.do?method=showUserRightList");
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return null;
	}
	
	public ActionForward createUserType(ActionMapping mapping,ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response){
		System.out.println("create user type");
		String typeName = request.getParameter("typeName");
		System.out.println("type name " + typeName);
		Membertype type = new Membertype();
		type.setMemtname(typeName);
		Global.getUserPermissionService().saveOrUpdate(type);
		try {
			response.sendRedirect("/userManage/userPermissionAction.do?method=showUserRightList");
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return null;
	}
	
	public ActionForward createUserLevel(ActionMapping mapping,ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response){
		String LevelName = request.getParameter("LevelName");
		Long LevelType = Long.parseLong(request.getParameter("LevelType"));
		System.out.println("name " + LevelName + " typeId " + LevelType);
		Memberlevel level = new Memberlevel();
		level.setMemlname(LevelName);
		Membertype type = new Membertype();
		type.setId(LevelType);
		level.setMembertype(type);
		Global.getUserPermissionService().saveOrUpdate(level);
		try {
			response.sendRedirect("/userManage/userPermissionAction.do?method=showUserRightList");
		} catch (Exception e) {
			throw new RuntimeException(e);
		}	
		return null;
	}
	
	public ActionForward getUserType(ActionMapping mapping,ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws IOException{
		System.out.println("get user type");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter returnValue = response.getWriter(); 
		String output = "";
		
		Membertype[] types = Global.getUserPermissionService().getAllUserType();
		for (int i = 0; i < types.length; i++) {
			output += "<option value='";
			output += types[i].getId();
			output += "'>";
			output += types[i].getMemtname();
			output += "</option>";
		}
		returnValue.print(output);   
		System.out.println(output);
		returnValue.flush();
		returnValue.close();  
		return null;
	}

}
